// +FHDR------------------------------------------------------------
//                 Copyright (c) 2022 .
//                       ALL RIGHTS RESERVED
// -----------------------------------------------------------------
// Filename      : spt_monitor.sv
// Author        : 
// Created On    : 2022-08-25 15:47
// Last Modified : 
// -----------------------------------------------------------------
// Description:
//
//
// -FHDR------------------------------------------------------------

`ifndef __SPT_MONITOR_SV__
`define __SPT_MONITOR_SV__

class spt_monitor extends uvm_monitor;
	virtual spt_interface vif;
	uvm_analysis_port #(spt_transaction) ap;
	
	`uvm_component_utils(spt_monitor)
	
	extern function new(string name = "spt_monitor", uvm_component parent=null);
	extern virtual function void build_phase(uvm_phase phase);
	extern virtual task main_phase(uvm_phase phase);
	extern virtual task receive_pkt();
endclass: spt_monitor

function spt_monitor::new(string name = "spt_monitor", uvm_component parent=null);
	super.new(name, parent);
endfunction: new

function void spt_monitor::build_phase(uvm_phase phase);
	super.build_phase(phase);
	`uvm_info("spt_monitor", "build_phase is called", UVM_HIGH);
	if(!uvm_config_db #(virtual spt_interface)::get(this, "", "vif", vif))begin
		`uvm_fatal("spt_monitor", "virtual interface get fatal");
	end
	ap   = new("ap", this);
endfunction: build_phase

task spt_monitor::main_phase(uvm_phase phase);
    while(!this.vif.rst_n) @this.vif.mon;
	fork 
		this.receive_pkt();
	join_none
endtask: main_phase

task spt_monitor::receive_pkt();
	spt_transaction data;
    int rcv_fsm = 0;
    bit [15:0]pkt_data[$];
	while(1) begin
		@this.vif.mon;
        if(this.vif.mon.valid == 1'b1)begin
            pkt_data.push_back(this.vif.mon.data);
            rcv_fsm = 1;
        end
        else if(rcv_fsm == 1 && this.vif.mon.valid == 1'b0)begin
            data = new();
            data.pkt_data = new[pkt_data.size()];
            foreach(pkt_data[i]) data.pkt_data[i] = pkt_data[i];
            data.unpack();
            ap.write(data);
            pkt_data.delete();
            rcv_fsm = 0;
        end
	end
endtask: receive_pkt

`endif
